package com.rgs.se.data.queue;


import com.rgs.se.data.array.Array;

/**
 * @author Ren Gaoshuai
 * @ClassName ArrayQueue
 * 数组实现队列
 * @date 2022-04-06 7:51
 **/
public class ArrayQueue<E> implements MyQueue<E>{

    Array<E> array;

    public ArrayQueue(){
        this(10);
   }

    public ArrayQueue(int capacity){
        array = new Array<E>(capacity);
    }

    @Override
    public void enQueue(E e) {
        array.add(e);
    }

    @Override
    public E deQueue() {
        return array.delete(0);
    }

    @Override
    public E getFront() {
        return array.index(0);
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Queue: front [");
        for (int i = 0; i < array.getSize(); i++) {
            builder.append(array.index(i));
            if (i != array.getSize() - 1) {
                builder.append(", ");
            }
        }
        builder.append("] tail");
        return builder.toString();
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> queue = new ArrayQueue<>();
        for (int i = 0;i< 10;i++){
            queue.enQueue(i);
            System.out.println(queue);

            if (i % 3 == 2){
                queue.deQueue();
            }
        }
        System.out.println("last:"+queue);
    }
}
